package com.acce.gxsc.page.registerCar;

import com.acce.gxsc.common.App;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.InCarIdBean;
import com.acce.gxsc.model.loaclBean.PregisterInfoBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.CarPredictionBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.base.volleyHttp.GsonUtils;
import com.acce.sdk.utils.LogUtils;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;

import rx.Subscriber;

/**
 * Created by huxley on 2017/5/26.
 */

public class RegisterCarPresenter extends BasePresenter<RegisterCarView> {


    public void deleteOrder(String pregisterKey) {
        WorkModel.getInstance().deleteOrder(pregisterKey).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("删除中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    LogUtils.logout(GsonUtils.toJson(datas));
                    getView().deleteOrderSuccess();
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("删除失败！");
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void inRoom(final CarPredictionBean carPredictionBean) {
        if (StringUtils.isEmpty(carPredictionBean.consignorNick) && StringUtils.isEmpty(carPredictionBean.declareCompanyNick)
                && StringUtils.isEmpty(carPredictionBean.owerName) && StringUtils.isEmpty(carPredictionBean.contactPhone)
                && StringUtils.isEmpty(carPredictionBean.plateNumX) && StringUtils.isEmpty(carPredictionBean.plateNumG)
                && StringUtils.isEmpty(carPredictionBean.preContainNo) && StringUtils.isEmpty(carPredictionBean.preContainSize)) {
            ToastUtils.show("不能提交空进场单");
            return;
        }
        if (StringUtils.isEmpty(carPredictionBean.pregisterKey)) {
            carPredictionBean.pregisterKey = DateUtils.getTime() + App.dutyId;
        }
        WorkModel.getInstance().inRoom(carPredictionBean).subscribe(new Subscriber<ResultBean<InCarIdBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("提交中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<InCarIdBean> datas) {
                if (mViewRef != null) {
                    LogUtils.logout(GsonUtils.toJson(datas));
                    ToastUtils.show("生成进场单成功");
                    getView().inRoomSuccess(datas.result.InCar, carPredictionBean);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("生成进场单失败");
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void registerCar(CarPredictionBean carPredictionBean) {
        if (StringUtils.isEmpty(carPredictionBean.consignorNick) && StringUtils.isEmpty(carPredictionBean.declareCompanyNick)
                && StringUtils.isEmpty(carPredictionBean.owerName) && StringUtils.isEmpty(carPredictionBean.contactPhone)
                && StringUtils.isEmpty(carPredictionBean.plateNumX) && StringUtils.isEmpty(carPredictionBean.plateNumG)
                && StringUtils.isEmpty(carPredictionBean.preContainNo) && StringUtils.isEmpty(carPredictionBean.preContainSize)) {
            ToastUtils.show("不能提交空报备单");
            return;
        }
        carPredictionBean.pregisterKey = DateUtils.getTime() + App.dutyId;
        WorkModel.getInstance().registerCar(carPredictionBean).subscribe(new Subscriber<ResultBean<Object>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<Object> datas) {
                if (mViewRef != null) {
                    if ("生成报备信息失败!".equals(datas.returnMsg)) {
                        ToastUtils.show("生成报备信息失败");
                    } else {
                        getView().registerCarSuccess(datas.returnMsg);
                    }
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    ToastUtils.show("生成报备信息失败");
                    getView().hideProgressDialog();
                }
            }
        });
    }

    public void loadRegisterCarDetail(String pregisterKey) {
        WorkModel.getInstance().loadRegisterCarDetail(pregisterKey).subscribe(new Subscriber<ResultBean<PregisterInfoBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<PregisterInfoBean> datas) {
                if (mViewRef != null) {
                    PregisterInfoBean result = datas.result;
                    CarPredictionBean pregisterInfo = result.PregisterInfo;
                    pregisterInfo.pregistergoods = result.PregisterGoods;
                    pregisterInfo.plateNumG = Tools.getStringNonNull(pregisterInfo.plateNumG);
                    pregisterInfo.plateNumX = Tools.getStringNonNull(pregisterInfo.plateNumX);
                    getView().loadRegisterCarSuccess(pregisterInfo);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
